<?php

namespace app\admin\controller;

use think\Validate;
use think\Db;
use app\models\{User as Users};

class Custom extends Base
{


    public function index()
    {
        if ($this->request->isAjax()) {
            $model = new Users();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['del', '=', 1];
            if (!empty($post['title'])) {
                $where[] = ['id|account|remrk', 'like', '%' . $post['title'] . '%'];
            }
            if (!empty($post['type'])) {
                $where[] = ['type', '=', $post['type']];
            }
            if (!empty($post['groups'])) {
                $where[] = ['', 'EXP', Db::raw("FIND_IN_SET(" . $post['groups'] . ",groups)")];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            foreach ($list as $row) {
                if ($row['groups'] == 99) {
                    $grouplist  = '全部项目';
                } else {
                    $groups = explode(',', $row['groups']) ?? [];
                    $grouplist = '';
                    foreach ($groups as $group) {
                        if (!empty($grouplist)) {
                            $grouplist  = $grouplist . '、' . (config()["config"]['type'][$group] ?? '');
                        } else {
                            $grouplist  = $grouplist . (config()["config"]['type'][$group] ?? '');
                        }
                    }
                }
                $row['groups_text'] = $grouplist;
                $row['type_name'] = config()["config"]['groups'][$row['type']] ?? '';
            }
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            $grouplist = config()["config"]['type'];
            $typelist = config()["config"]['groups'];
            $grouplist[99] = '全部组别';
            return view(null, ['grouplist' => $grouplist, 'typelist' => $typelist]);
        }
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'account' => 'require',
                'remrk' => 'require',
                'type' => 'require',
                'groups' => 'require',
            ], [
                'account.require' => '账号不能为空',
                'remrk.require' => '备注不能为空',
                'type.require' => '职位类型不能为空',
                'groups.require' => '组别不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['code' => 1, 'msg' =>  $validate->getError()]);
            }
            //查找账号
            $have = (new Users())->where('del', 1)->where('account', $data['account'])->find();
            if (!empty($have) && $have['id'] != $data['id']) {
                return json(['code' => 1, 'msg' => '账号已存在']);
            }
            if (empty($data['id']) && empty($data['password'])) {
                return json(['code' => 1, 'msg' => '请填写密码']);
            }
            if (!empty($data['password'])) {
                $data['password'] = md5(md5($data['password']));
            } else {
                unset($data['password']);
            }
            if (count($data['groups']) <= 0) {
                return json(['code' => 1, 'msg' => '分组必须选择']);
            }
            if ($data['type'] == 2 && count($data['groups']) > 1) {
                return json(['code' => 1, 'msg' => '售后人员只能绑定一个分组']);
            }
            if (in_array(99, $data['groups'])) {
                $data['groups'] = 99;
            } else {
                $data['groups'] = implode(',', $data['groups']);
            }

            if (empty($data['id'])) {
                $res = (new Users())->allowfield(true)->save($data);
            } else {
                $res = (new Users())->allowfield(true)->save($data, ['id' => $data['id']]);
            }
            if ($res) {
                return json(['code' => 200, 'status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['code' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Users())->where('id', $id)->find();
            if ($row) {
                $row['groups'] = explode(',', $row['groups']) ?? [];
            }
            $grouplist = config()["config"]['type'];
            $typelist = config()["config"]['groups'];
            $grouplist[99] = '全部组别(选中此项可查看全部项目)';
            return view('', [
                'row' => $row,
                'grouplist' => $grouplist,
                'typelist' => $typelist
            ]);
        }
    }

    public function del()
    {
        $admin_id =  $this->request->get('id');
        $admin = (new Users())->where('id', $admin_id)->find();
        if (empty($admin)) {
            return json(['status' => 1, 'msg' => '账号不存在']);
        }
        if ($admin['del'] == 1) {
            $del = 2;
        } else {
            $del = 1;
        }
        $res = (new Users())->where('id', $admin_id)->update(['del' => $del]);
        if ($res) {
            return json(['code' => 200, 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'msg' => '操作失败']);
    }
}
